﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MathLab.Messaging;

namespace MathLab.Utils
{
    class StringUtils
    {
        public static string TrimBlanks(string str)
        {
            StringBuilder sb = new StringBuilder();

            bool arrayFlag = false;

            for (int i = 0; i < str.Length; i++)
            {
                char c = str[i];

                if (Literals.ARRAY_DECLARATION_START.Equals(c.ToString()))
                {
                    arrayFlag = true;
                }
                if (Literals.ARRAY_DECLARATION_END.Equals(c.ToString()))
                {
                    arrayFlag = false;
                }

                if (!arrayFlag)
                {
                    switch (c)
                    {
                        case '\r':
                        case '\n':
                        case '\t':
                        case ' ':
                            continue;
                        default:
                            sb.Append(c);
                            break;
                    }
                }
                else
                {
                    if (
                        !(
                            (c.ToString().Equals(Literals.ARRAY_ELEMENT_SEPARATOR)) 
                            && 
                            (str[i - 1].ToString().Equals(Literals.ARRAY_ROW_END))
                         )
                       )
                    {
                        sb.Append(c);
                    }
                }
            }
            return sb.ToString();
        }

        public static string arrayToString(Matrix inputArray)
        {
            double[,] array = inputArray.matrix;
            string result = "";
            int[] longestNumbers = new int[array.GetUpperBound(1) + 1];

            for (int i = 0; i < longestNumbers.Length; i++)
            {
                longestNumbers[i] = 0;
            }

            for (int i = array.GetLowerBound(0); i <= array.GetUpperBound(0); i++)
            {
                for (int j = array.GetLowerBound(1); j <= array.GetUpperBound(1); j++)
                {
                    longestNumbers[j] = Math.Max(array[i, j].ToString().Length, longestNumbers[j]);
                }
            }

            for (int i = array.GetLowerBound(0); i <= array.GetUpperBound(0); i++)
            {
                result += Literals.ARRAY_BOUND;

                for (int j = array.GetLowerBound(1); j <= array.GetUpperBound(1); j++)
                {
                    result += array[i, j].ToString();

                    for (int k = 0; k < longestNumbers[j] - array[i, j].ToString().Length; k++)
                    {
                        result += Literals.ARRAY_ELEMENT_SEPARATOR;
                    }

                    if (j != array.GetUpperBound(1))
                    {
                        result += Literals.ARRAY_ELEMENT_SEPARATOR;
                    }
                }
                result += Literals.ARRAY_BOUND + "\n";
            }

            return result;
        }

        public static string arrayToString(double[,] inputArray)
        {
            double[,] array = inputArray;
            string result = "";
            int[] longestNumbers = new int[array.GetUpperBound(1) + 1];

            for (int i = 0; i < longestNumbers.Length; i++)
            {
                longestNumbers[i] = 0;
            }

            for (int i = array.GetLowerBound(0); i <= array.GetUpperBound(0); i++)
            {
                for (int j = array.GetLowerBound(1); j <= array.GetUpperBound(1); j++)
                {
                    longestNumbers[j] = Math.Max(array[i, j].ToString().Length, longestNumbers[j]);
                }
            }

            for (int i = array.GetLowerBound(0); i <= array.GetUpperBound(0); i++)
            {
                result += Literals.ARRAY_BOUND;

                for (int j = array.GetLowerBound(1); j <= array.GetUpperBound(1); j++)
                {
                    result += array[i, j].ToString();

                    for (int k = 0; k < longestNumbers[j] - array[i, j].ToString().Length; k++)
                    {
                        result += Literals.ARRAY_ELEMENT_SEPARATOR;
                    }

                    if (j != array.GetUpperBound(1))
                    {
                        result += Literals.ARRAY_ELEMENT_SEPARATOR;
                    }
                }
                result += Literals.ARRAY_BOUND + "\n";
            }

            return result;
        }

        public static string arrayToFileString(Matrix inputArray)
        {
            double[,] array = inputArray.matrix;
            string result = inputArray.name + Literals.EQUALS;

            result += Literals.ARRAY_DECLARATION_START;

            for (int i = array.GetLowerBound(0); i <= array.GetUpperBound(0); i++)
            {
                for (int j = array.GetLowerBound(1); j <= array.GetUpperBound(1); j++)
                {
                    result += array[i, j].ToString();

                    if (j != array.GetUpperBound(1))
                    {
                        result += Literals.ARRAY_ELEMENT_SEPARATOR;
                    }
                }

                if (i != array.GetUpperBound(0))
                {
                    result += Literals.ARRAY_ROW_END;
                }
            }

            result += Literals.ARRAY_DECLARATION_END;

            return result;
        }
    }        
}
